package com.zhou.config;

import feign.Contract;
import feign.Feign;
import feign.auth.BasicAuthRequestInterceptor;
import feign.httpclient.ApacheHttpClient;
import org.springframework.cloud.netflix.feign.ribbon.CachingSpringLoadBalancerFactory;
import org.springframework.cloud.netflix.feign.ribbon.LoadBalancerFeignClient;
import org.springframework.cloud.netflix.feign.support.SpringMvcContract;
import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class FeignConfiguration {

    @Bean
    public BasicAuthRequestInterceptor basicAuthRequestInterceptor() {
        return new BasicAuthRequestInterceptor("user", "password");
    }

    /**
     * 使用SpringMVC的形式来进行注释翻译
     */
    @Bean
    public Contract springMvcContract(){
        return new SpringMvcContract();
    }

    @Bean
    public SpringClientFactory springClientFactory() {
        return new SpringClientFactory();
    }

    /**
     * 用于进行直接调用的client
     */
    @Bean(name = "ABClient")
    public LoadBalancerFeignClient client (SpringClientFactory clientFactory) {
        ApacheHttpClient httpClient = new ApacheHttpClient();
        return new LoadBalancerFeignClient(httpClient,
                new CachingSpringLoadBalancerFactory(clientFactory), clientFactory);
    }

    /**
     * 配置feignBuilder
     */
    @Bean
    public Feign.Builder feignBuilder(LoadBalancerFeignClient client) {
        return Feign.builder()
                .client(client);
    }

}
